<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="接口ID" prop="interfaceId">
        <el-input
          v-model="queryParams.interfaceId"
          placeholder="请输入接口ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数名" prop="fieldId">
        <el-input
          v-model="queryParams.fieldId"
          placeholder="请输入参数名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父参数名" prop="parentFieldId">
        <el-input
          v-model="queryParams.parentFieldId"
          placeholder="请输入父参数名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数中文名" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入参数中文名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否必填" prop="isRequired">
        <el-input
          v-model="queryParams.isRequired"
          placeholder="请输入是否必填"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择数据类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="长度" prop="fieldLength">
        <el-input
          v-model="queryParams.fieldLength"
          placeholder="请输入长度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="说明" prop="fieldExplain">
        <el-input
          v-model="queryParams.fieldExplain"
          placeholder="请输入说明"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['upload:interfaceFields:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="interfaceFieldsList"
      row-key="fieldId"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="参数中文名" align="center" prop="fieldName" />
      <el-table-column label="参数名" prop="fieldId" />
      <el-table-column label="是否必填" align="center" prop="isRequired" />
      <el-table-column label="数据类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="长度" align="center" prop="fieldLength" />
      <el-table-column label="说明" align="center" prop="fieldExplain" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['upload:interfaceFields:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['upload:interfaceFields:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['upload:interfaceFields:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改接口字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接口ID" prop="interfaceId">
          <el-input v-model="form.interfaceId" placeholder="请输入接口ID" />
        </el-form-item>
        <el-form-item label="参数名" prop="fieldId">
          <el-input v-model="form.fieldId" placeholder="请输入参数名" />
        </el-form-item>
        <el-form-item label="父参数名" prop="parentFieldId">
          <treeselect v-model="form.parentFieldId" :options="interfaceFieldsOptions" :normalizer="normalizer" placeholder="请选择父参数名" />
        </el-form-item>
        <el-form-item label="参数中文名" prop="fieldName">
          <el-input v-model="form.fieldName" placeholder="请输入参数中文名" />
        </el-form-item>
        <el-form-item label="是否必填" prop="isRequired">
          <el-input v-model="form.isRequired" placeholder="请输入是否必填" />
        </el-form-item>
        <el-form-item label="数据类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择数据类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="长度" prop="fieldLength">
          <el-input v-model="form.fieldLength" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="说明" prop="fieldExplain">
          <el-input v-model="form.fieldExplain" placeholder="请输入说明" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInterfaceFields, getInterfaceFields, delInterfaceFields, addInterfaceFields, updateInterfaceFields, exportInterfaceFields } from "@/api/ncpcs/interfaceFields";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "InterfaceFields",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 接口字段表格数据
      interfaceFieldsList: [],
      // 接口字段树选项
      interfaceFieldsOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据类型字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        id: null,
        interfaceId: null,
        fieldId: null,
        parentFieldId: null,
        fieldName: null,
        isRequired: null,
        type: null,
        fieldLength: null,
        fieldExplain: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("data_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询接口字段列表 */
    getList() {
      this.loading = true;
      listInterfaceFields(this.queryParams).then(response => {
        this.interfaceFieldsList = this.handleTree(response.data, "fieldId", "parentFieldId");
        this.loading = false;
      });
    },
    /** 转换接口字段数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.fieldId,
        label: node.fieldId,
        children: node.children
      };
    },
	/** 查询接口字段下拉树结构 */
    getTreeselect() {
      listInterfaceFields().then(response => {
        this.interfaceFieldsOptions = [];
        const data = { fieldId: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "fieldId", "parentFieldId");
        this.interfaceFieldsOptions.push(data);
      });
    },
    // 数据类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        interfaceId: null,
        fieldId: null,
        parentFieldId: null,
        fieldName: null,
        isRequired: null,
        type: null,
        fieldLength: null,
        fieldExplain: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.fieldId) {
        this.form.parentFieldId = row.fieldId;
      } else {
        this.form.parentFieldId = 0;
      }
      this.open = true;
      this.title = "添加接口字段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentFieldId = row.fieldId;
      }
      getInterfaceFields(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改接口字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInterfaceFields(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInterfaceFields(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除接口字段编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInterfaceFields(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
  }
};
</script>
